//
//  Node.m
//  iFlickr
//
//  Created by 张 霄鹤 on 09-7-5.
//  Copyright 2009 xhzhang. All rights reserved.
//

#import "Node.h"


@implementation Node

@synthesize parent;

- (id)init{
	if(self = [super init]){
		childs = [[NSMutableArray alloc] init];
		attributes = [[NSMutableDictionary alloc] init];
	}
	return self;
}

- (NSInteger)nodeType{
	return NONE_NODE;
}

- (BOOL)appendChild:(Node *)child{
	[childs addObject:child];
	child.parent = self;
	return YES;
}

/**
 * 获取节点名称，所有继承自该类的类，都要定义方法，来重载
 */
- (NSString *)getNodeName{
	return @"#NODE";
}


/**
 * 获取第一个子节点
 */
- (Node *)getFirstChild{
	Node *ret = nil;
	if([childs count] > 0){
		ret = [childs objectAtIndex:0];
	}
	return ret;
}

/**
 * 获取最后一个子节点
 */
- (Node *)getLastChild{
	Node *ret = nil;
	NSInteger len = [childs count];
	if(len > 0){
		ret = [childs objectAtIndex:(len - 1)];
	}
	return ret;
}

/**
 * 获取前一个和自己在平级关系上的节点
 */
- (Node *)getPreviousSibling{
	Node *ret = nil;
	if(parent != nil){
		NSArray *cs = [parent getChildNodes];
		NSInteger index = [cs indexOfObject:self];
		if(index > 0){
			ret = [cs objectAtIndex:(index - 1)];
		}
	}
	return ret;
}

/**
 * 获取后一个和自己在平级关系上的节点
 */
- (Node *)getNextSibling{
	Node *ret = nil;
	if(parent != nil){
		NSArray *cs = [parent getChildNodes];
		NSInteger index = [cs indexOfObject:self];
		NSInteger len = [cs count];
		if(index > 0 && index < (len - 1)){
			ret = [cs objectAtIndex:(index + 1)];
		}
	}
	return ret;
}

/**
 * 获取子节点
 */
- (NSArray *)getChildNodes{
	NSArray *ret = nil;
	if([childs count] > 0){
		//ret = [NSArray arrayWithArray:childs];
		ret = childs;
	}
	return ret;
}

- (Node *)getChildByName:(NSString *)nodeName{
	Node *ret = nil;
	if(nodeName){
		int len = [childs count];
		Node *n1;
		NSString *en;
		for(int i = 0;i<len;i++){
			n1 = [childs objectAtIndex:i];
			en = [n1 getNodeName];
			if([en isEqualToString:nodeName]){
				ret = n1;
				break;
			}
		}
	}
	return ret;
}

- (NSArray *)getChildsByName:(NSString *)nodeName{
	NSMutableArray *ret = nil;
	if(nodeName){
		ret = [NSMutableArray array];
		int len = [childs count];
		Node *n1;
		NSString *en;
		for(int i = 0;i<len;i++){
			n1 = [childs objectAtIndex:i];
			en = [n1 getNodeName];
			if([en isEqualToString:nodeName]){
				[ret addObject:n1];
			}
		}
	}
	return ret;
}

- (BOOL)hasChild{
	return [childs count] > 0;
}


- (NSDictionary *)getAttributes{
	NSDictionary *ret = nil;
	// TODO 主要是避免结束元素还要返回 attribute
	if([attributes count] > 0){
		ret = attributes; //[NSDictionary dictionaryWithDictionary:attributes];
	}
	return ret;
}

/**
 * 设置属性
 */
- (void)setAttributes:(NSDictionary *)attributeDict{
	if(attributeDict){
		// 旧的数据被 removeAll 新的被赋值过来
		[attributes setDictionary:attributeDict];
	}
}

/**
 * 获取属性，如果不存在，那么返回 nil
 */
- (NSString *)getAttribute:(NSString *)name{
	NSString *ret = nil;
	if([attributes count] > 0){
		ret = [attributes objectForKey:name];
	}
	return ret;
}

- (void)setAttribute:(NSString *)key withValue:(NSString *)value{
	[attributes setObject:value forKey:key];
}

- (BOOL)hasAttributes{
	return [attributes count]>0;
}

- (NSString *)getValue{
	return nil;
}

/**
 * 查找 xpath 节点 指定 从 根节点开始
 */
-(Node *)query:(NSString *)xpath{
	NSArray *paths = [xpath componentsSeparatedByString:@"/"];
	int con = [paths count];
	Node *ret = nil;
	if(con > 0){
		NSString *ename;
		int len;
		Node *cur = self;
		// char ch;
		NSArray *chds;
		// 从1 开始，因为 rootElement 已经获取一层了
		for (int i = 0; i<con; i++) {
			ename = [paths objectAtIndex:i];
			len = [ename length];
			if(len > 0){
				// ch = [ename characterAtIndex:0];
				int index = 0;
				NSRange range = [ename rangeOfString:@"["];
				if(range.location != NSNotFound){
					int sindex = range.location;
					// TODO 找到了
					NSRange rend = [ename rangeOfString:@"]"];
					range.location = range.location + range.length;
					range.length = rend.location - range.location;
					NSString *substr = [ename substringWithRange:range];
					if(substr){
						index = [substr intValue];
					}
					// 截至  sindex 之前
					ename = [ename substringToIndex:sindex];
				}
				// TODO 做简单一点，就只能够按照顺序搜索
				chds = [cur getChildsByName:ename];
				if(chds){
					if([chds count]>0){
						cur = [chds objectAtIndex:index];
					}
				}else{
					// TODO 没有找到
					cur = nil;
					break;
				}
			}
		}
		ret = cur;
	}
	return ret;
}

- (NSString *)toXML{
	// TODO 所有子类均应该继承
	return @"";
}


- (void)dealloc{
	[childs removeAllObjects];
	[childs release];
	[attributes release];
	[super dealloc];
}

@end
